package com.feeyin.radar.commons;

import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils
{
  public static String getDateByFormat(String strFormat)
  {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
    Date date = new Date();
    return simpleDateFormat.format(date);
  }

  public static Date getDateByFormat(Date date, String strFormat)
  {
    if (date == null) return null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(date));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static String getStringDateByFormat(Date date, String strFormat)
  {
    if (date == null) return null;
    if (StringUtils.isEmpty(strFormat)) return null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
    String strDate = "";
    try {
      strDate = simpleDateFormat.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return strDate;
  }

  public static Date stringDateConvertDate(String stringDate, String strFormat)
  {
    Date date = null;
    try {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
      date = simpleDateFormat.parse(stringDate);
    } catch (Exception ex) {
      return date;
    }
    return date;
  }

  public static Date getDateSubtractDay(Date date, long day)
  {
    if (date == null) return null;
    date = new Date(date.getTime() - day * 24L * 60L * 60L * 1000L);
    return date;
  }

  public static long getHourByDate(Date paraDate)
  {
    if (paraDate == null) return 0L;
    Date date = new Date();
    long time = date.getTime() - paraDate.getTime();
    if (time == 0L) return 0L;
    return time / 1000L / 60L / 60L;
  }

  public static long getDayByDate(Date paraDate)
  {
    if (paraDate == null) return 0L;
    Date date = new Date();
    if (paraDate.compareTo(date) >= 0) return 0L;
    long time = date.getTime() - paraDate.getTime();
    if (time == 0L) return 0L;
    return time / 1000L / 60L / 60L / 24L;
  }

  public static long getDayByTwoDate(Date date1, Date date2)
  {
    if (date1 == null) return 0L;
    if (date2 == null) return 0L;
    if (date1.compareTo(date2) <= 0) return 0L;

    long time = date1.getTime() - date2.getTime();
    if (time == 0L) return 0L;
    return time / 1000L / 60L / 60L / 24L;
  }

  public static Date getDateAddDay(Date date, int day)
  {
    if (date == null) return null;
    if (day <= 0) return date;
    long time = date.getTime();
    time += 86400000L;
    date = new Date(time);

    return date;
  }

  public static int getCurrentMonthSeason(int sign)
  {
    int num = 0;
    Calendar calendar = Calendar.getInstance();
    if (sign == 1)
      num = calendar.get(2) + 1;
    else if (sign == 2) {
      num = getSeasonByMonth(calendar.get(2) + 1);
    }
    return num;
  }

  public static int getSeasonByMonth(int month)
  {
    int season = 1;
    if ((month >= 1) && (month <= 3)) {
      season = 1;
    }
    if ((month >= 4) && (month <= 6)) {
      season = 2;
    }
    if ((month >= 7) && (month <= 9)) {
      season = 3;
    }
    if ((month >= 10) && (month <= 12)) {
      season = 4;
    }
    return season;
  }

  public static List getOneWeekDate()
  {
    List oneWeekDates = new ArrayList();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    for (int i = 0; i < 7; i++) {
      calendar.set(7, 2 + i);
      Date date = null;
      try {
        date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
        oneWeekDates.add(date);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return oneWeekDates;
  }

  public static List getCurrentMonthDate()
  {
    List dates = new ArrayList();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();

    int dayNum = getCurrentMonthDayNum();
    for (int i = 1; i <= dayNum; i++) {
      calendar.set(5, i);
      Date date = null;
      try {
        date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
      } catch (ParseException e) {
        e.printStackTrace();
      }
      dates.add(date);
    }
    return dates;
  }

  public static int getCurrentMonthDayNum()
  {
    Calendar calendar = Calendar.getInstance();

    calendar.set(2, calendar.get(2) + 1);
    calendar.set(5, 1);
    calendar.set(5, calendar.get(5) - 1);
    return calendar.get(5);
  }

  public static Date getCurrentMonthFirstDay()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentMonthEndDay()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    calendar.roll(5, -1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentSeasonFirstDay()
  {
    int[][] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
    int season = 1;
    Calendar calendar = Calendar.getInstance();
    int month = calendar.get(2) + 1;
    if ((month >= 1) && (month <= 3)) {
      season = 1;
    }
    if ((month >= 4) && (month <= 6)) {
      season = 2;
    }
    if ((month >= 7) && (month <= 9)) {
      season = 3;
    }
    if ((month >= 10) && (month <= 12)) {
      season = 4;
    }
    int start_month = array[(season - 1)][0];

    calendar.set(2, start_month - 1);
    calendar.set(5, 1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentSeasonEndDay()
  {
    int[][] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
    int season = 1;
    Calendar calendar = Calendar.getInstance();
    int month = calendar.get(2) + 1;
    if ((month >= 1) && (month <= 3)) {
      season = 1;
    }
    if ((month >= 4) && (month <= 6)) {
      season = 2;
    }
    if ((month >= 7) && (month <= 9)) {
      season = 3;
    }
    if ((month >= 10) && (month <= 12)) {
      season = 4;
    }

    int end_month = array[(season - 1)][2];
    calendar.set(2, end_month - 1);
    calendar.set(5, 1);
    calendar.roll(5, -1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentYearFirstDay()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(2, 0);
    calendar.set(5, 1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static Date getCurrentYearEndDay()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(2, 11);
    calendar.set(5, 1);
    calendar.roll(5, -1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = simpleDateFormat.parse(simpleDateFormat.format(calendar.getTime()));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  public static void main(String[] args)
  {
  //  DateUtils.getIntervalDate(date1, date2)
  }

public static Integer getIntervalDate(Date date1, Date date2) {
		int days = 0;
		days = (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
		return days;
}
}